#include "client.h"

void menu(){
    printf("\n---------------------------------\n");
    printf("               0---注册            \n");
    printf("               1---登录            \n");
    printf("-----------------------------------\n");
}
// 用户注册
int signUp(int *sockfd, userInfo_t *user) {
    char userName[128] = {0};
    char passWord001[128] = {0};
    char passWord002[128] = {0};
    train_t train;
    //随机生成盐值并存储，因为随机生成盐值的函数限制在一定字符范围之内，绝对不会超过128
    //用户开始输入
    printf("请输入账号名:\n");
    scanf("%s", userName);
    printf("请输入密码:\n");
    scanf("%s", passWord001);
    printf("请再次输入密码:\n");
    scanf("%s", passWord002);
    // 对比两次输入密码是否一致，一致，则发送给服务端进行存储.
    if (strncmp(passWord001, passWord002, 128) == 0) {
        strcpy(user->userName, userName);
        printf("%s\n", user->userName);//打印userName
        //把明文密码存放到用户信息中
        strcpy(user->passWord, passWord002);
        printf("%s\n", user->passWord); // 打印passWord
        
        //客户端第二次发送，因为是服务端生产盐值，所以，传输用户名和明文密码即可
        memset(train.data, 0, sizeof(train.data));
        train.length = sizeof(userInfo_t);
        memcpy(train.data, user, train.length);
        printf("userNmae = %s\npassWord = %s\n", user->userName, user->passWord);
        send(*sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);

        // 客户端第一次接收，接收服务器消息，注册是否成功
        memset(&train, 0, sizeof(train));
        recv(*sockfd, &train.length, sizeof(int), MSG_WAITALL);
        ssize_t sret = recv(*sockfd, train.data, train.length, MSG_WAITALL);
        if (sret == 3 && strcmp(train.data, "-1") == 0) {
            printf("注册失败，请重新注册！\n");
            return -1;
        } else if (sret == 2 && strcmp(train.data, "1") == 0) {
            printf("用户名已存在，请重新注册！\n");
            return -1;
        }else{
            return 0;
        }
    }
    else{//客户端注册输入的两次密码不一样，直接注册失败，这种低级操作，就别折磨服务端了
        memset(&train, 0, sizeof(train));
        char buf[] = "-10";
        train.length = sizeof(buf);
        memcpy(train.data, buf, train.length);
        send(*sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);
        printf("前后输入的密码不一样，请重新注册！\n");
        
        memset(&train, 0, sizeof(train));
        recv(*sockfd, &train.length, sizeof(int), MSG_WAITALL);
        recv(*sockfd, train.data, train.length, MSG_WAITALL);
        return -1;
    }
}
// 用户登录
int signIn(int *sockfd, userInfo_t *user) {
    train_t train;
    // 没事初始化一下
    memset(&train, 0, sizeof(train));
    char userName[128] = {0};
    char passWord001[128] = {0};

    printf("请输入账号名:\n");
    scanf("%s", userName);
    printf("请输入密码:\n");
    scanf("%s", passWord001);

    // 登录，客户端第二次发送，发送 用户名+明文密码
    strncpy(user->userName, userName, strlen(userName));
    strncpy(user->passWord, passWord001, strlen(passWord001));
    train.length = sizeof(userInfo_t);
    memcpy(train.data, user, train.length);
    send(*sockfd, &train, sizeof(int) + train.length, MSG_NOSIGNAL);
    memset(&train, 0, sizeof(train));
    printf("第一处路障通过-无！\n");
    
    //客户端第一次接收在，直接判定所有！
    recv(*sockfd, &train.length, sizeof(int), MSG_WAITALL);
    ssize_t sret = recv(*sockfd, train.data, train.length, MSG_WAITALL);
    printf("%s\n", train.data);
    printf("第一次接收train.data = %s\n", train.data);
    //如果客户端发过来的信息，是没有该用户名，则直接登录失败，当场退出
    if(sret == 3 && strcmp(train.data, "-2") == 0){
        printf("用户不存在，请重新登录！\n");
        return -1;
    }
    else if(sret == 2 && strcmp(train.data, "0") == 0){
        printf("登录成功！\n");
        return 0;
    }
    else{
        printf("密码输入错误，请重新登录！\n");
        return -1;
    }
}
// 用户注册/登录中转站
int logIn(int *sockfd, userInfo_t *user) {
    int loginCmd = 0;
    train_t train;
    memset(&train, 0, sizeof(train));
    //直接进入循环，毕竟注册登录可能失败，要再来几遍
    while(1){
        printf("请选择您的操作！\n");
        memset(&train, 0, sizeof(train));
        //char cmd[64] = {0};
        printf("ready go!\n");
        menu();
        //printf("请输入您的选择\n注册---0---\n登录---1---\n");
        //fflush(stdin);
        //while(getchar() != '\n');
        //fgets(cmd, sizeof(cmd), stdin);
        int cmd1 = 0;
        scanf("%d", &cmd1);
        //cmd[strcspn(cmd, "\n")] = '\0';//去除fgets读取的换行符
        //printf("after cmd = %s检查换行符\n", cmd);
        char cmd[20] = {0};
        sprintf(cmd, "%d", cmd1);
        printf("cdm = %s\n", cmd);

        if(strcmp(cmd, "0") == 0){
            //先发送过去SIGN_UP，客户端第一次发送
            loginCmd = SIGN_UP;
            printf("loginCmd = %d\n", loginCmd);
            train.length = sizeof(int);
            sprintf(train.data, "%d", loginCmd);
            printf("客户端第一次发送，通过train.data发送 SIGN_UP = %s\n", train.data);
            send(*sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);
            
            //进行注册
            int ret = signUp(sockfd, user);
            if(ret == -1 || ret == 1){
                continue;
            }
            //到这里，表示注册成功
            printf("恭喜你，注册成功！\n您的登录账户名是 %s\n", user->userName);
            return 0;//到这里，用户注册成功,函数返回
        }else if(strcmp(cmd, "1") == 0){
            //先发送过去SIGN_IN，客户端第一次发送
            loginCmd = SIGN_IN;
            printf("loginCmd = SIGN_IN = %d\n", loginCmd);
            train.length = sizeof(loginCmd);
            sprintf(train.data, "%d", loginCmd);
            printf("train.data = %s\n", train.data);
            send(*sockfd, &train, sizeof(int)+train.length, MSG_NOSIGNAL);

            //进行登录
            int ret = signIn(sockfd, user);
            printf("ret = %d\n", ret);
            if (ret == -1) {
                continue;
            }
            // 走到这里，表示登录成功
            printf("登录成功！\n");
            return 0;
        } 
        printf("invalid input!\n");
    }
    return 0;
}
